package outlookBar;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;

/**
 * Just a simple outlook-like toolbar
 * @author Carlos E. Descalzi
 */
public class OutlookBar extends JComponent {
	public OutlookBar(){
		setLayout(new Layout());
	}
	
	private ActionListener listener = new ActionListener(){
		public void actionPerformed(ActionEvent event){
			select((BarSection)event.getSource());
		}
	};

	/**
	 * Adds a component creating a specific section for it
	 * @param title title of the section
	 * @param component the component to add
	 */
	public void addComponent(String title,JComponent component){
		addComponent(title,null,component);
	}

	/**
	 * Adds a component creating a specific section for it
	 * @param title title of the section
	 * @param icon the icon of the section
	 * @param component the component to add
	 **/
	public void addComponent(String title,Icon icon,JComponent component){
		addComponent(new DefaultBarHeader(title,icon),component);
	}

	/**
	 * Adds a component creating a specific section for it, with a custom header
	 * @param header the section header
	 * @param component the component to add
	 **/
	public void addComponent(BarHeader header,JComponent component){
		BarSection section = new BarSection(header,component);
		section.addActionListener(listener);
		add(section);
		
		if (getComponentCount() == 1){
			select(section);
		}
		validate();
	}
	
	
	public void removeComponent(JComponent jComponent){
		for (Component component:getComponents()){
			BarSection section = (BarSection)component;
			
			if (section.getComponent().equals(jComponent)){
				remove(section);
				break;
			}
		}
		validate();
	}
	
	private void select(BarSection section){
		
		for (BarSection aSection:getSections()){
			if (aSection.equals(section)){
				aSection.setComponentVisible(true);
			} else {
				aSection.setComponentVisible(false);
			}
		}
		
		validate();
	}
	
	public List<BarSection> getSections(){
		List<BarSection> sections = new ArrayList<BarSection>();
		
		for (Component component:getComponents()){
			sections.add((BarSection)component);
		}
		
		return sections;
	}

	private class Layout implements LayoutManager {
		public void addLayoutComponent(String name, Component comp) {}
		public void removeLayoutComponent(Component comp) {}

		public Dimension preferredLayoutSize(Container parent) {
			return parent.getSize();
		}

		public Dimension minimumLayoutSize(Container parent) {
			return parent.getMinimumSize();
		}

		public void layoutContainer(Container parent) {
			Dimension size = parent.getSize();
			
			List<BarSection> sections = getSections();
		
			int totalHeaderHeight = 0;
			
			for (BarSection section:sections){
				totalHeaderHeight+=section.getHeaderDimension().height;
			}
			
			int yOffset = 0;
			
			for (BarSection section:sections){
				int height = 0;
				int headerHeight = section.getHeaderDimension().height;

				if (!section.isComponentVisible()){
					height = headerHeight;
					section.setBounds(
						0,	yOffset,
						size.width,	height);
				} else {
					height = size.height - (totalHeaderHeight - headerHeight);
					
					section.setBounds(
						0,	yOffset,
						size.width,	height);
				}
				yOffset+=height;
			}
		}
	}
	
}